package com.hotel.front;

import java.io.IOException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.google.appengine.api.datastore.EntityNotFoundException;
import com.google.appengine.labs.repackaged.org.json.JSONException;
import com.google.gson.Gson;
import com.hotel.BL.CounterLogic;
import com.hotel.BL.CounterPaymentLogic;
import com.hotel.BL.GenericLogic;
import com.hotel.BL.PayTypeLogic;

import data.Counter;
import data.CounterPayment;
import data.PayType;
import data.Payment;

@SuppressWarnings("serial")
public class CounterPaymentService extends HttpServlet {
	//http://localhost:8080/counterpaymentservice?func=changePayType&paytypeId=865
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		try{
			String doClass = req.getParameter("func");
			if(doClass.equals("CreateCounterPayment")){
				CreateCounterPayment(req,resp);
			}else if(doClass.equals("getLastPaymentData")){
				GetLastPaymentData(req,resp);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void CreateCounterPayment(HttpServletRequest req, HttpServletResponse resp) 
			throws IOException, EntityNotFoundException, JSONException, ServletException, 
			ParseException{
		HttpSession session=req.getSession(true);
		long hotelId = (Long) session.getAttribute("hotelId");
		double amount = Double.parseDouble(req.getParameter("amount"));
		String counter = req.getParameter("currentCounter");
		GenericLogic g = new GenericLogic(req,resp);
		new CounterPaymentLogic().AddCounterPayment(hotelId, counter,
				amount, g.ServerDateTime(hotelId), g.ServerDateFromNightAudit(hotelId), amount);
		CounterLogic c = new CounterLogic();
		Counter counterData = c.GetCounter(Long.parseLong(counter));
		counterData.setOpen(true);
		c.UpdateCounter(counterData);
		new LoginService().UpdateCounter(req,resp);
	}
	
	public void GetLastPaymentData(HttpServletRequest req, HttpServletResponse resp) throws IOException
	{
		HttpSession session=req.getSession(true);
		long hotelId = (Long) session.getAttribute("hotelId");
		String counter = req.getParameter("counter");
		List<PayType> paytype = new PayTypeLogic().GetAllPayType(hotelId);
		CounterPayment cp = new CounterPaymentLogic().GetLastCounterPayment(hotelId, counter);
		List<Payment> paymentList = new ArrayList<Payment>();
		if(cp == null){
			paymentList = null;
		}else{
			paymentList = cp.getPayment();
		}
		List<HashMap<String, Object>> list = new ArrayList<HashMap<String,Object>>();
		for(int i=0;i< paytype.size();i++){
			HashMap<String, Object> x = new HashMap<String, Object>();
			x.put("title", paytype.get(i).getTitle());
			double amount = 0;
			if(paymentList != null){
				for(int c = 0;c<paymentList.size();c++){
					if(paymentList.get(c).getPayType().getId() == paytype.get(i).getId()){
						amount += paymentList.get(c).getAmount();
						paymentList.remove(c);
						c--;
					}
				}
			}
			x.put("amount", amount);
			list.add(x);
		}
		HashMap<String, Object> data = new HashMap<String, Object>();
		data.put("data", list);
		data.put("symbol", new GenericLogic(req, resp).GetGeneral(hotelId).getDefaultCurrency().getSymbol());
		Gson gson = new Gson();

		resp.setContentType("text/plain; charset=UTF-8");
		resp.getWriter().println(gson.toJson(data));
	}
}
